﻿using System;
using System.Text;
using System.Collections;
using System.Configuration;
using System.Xml;
using System.Collections.Specialized;
namespace Story.Library
{
    public static class ConfigHelper
    {
        #region - ReadSetting -
        /// <summary>
        ///     String version of ReadSetting.
        ///     <remarks>
        ///         Reads a setting from a hashtable and converts it to the correct
        ///         type. One of these functions is provided for each type
        ///         expected in the hash table. These are public so that other
        ///         classes don't have to duplicate them to read settings from
        ///         a hash table.
        ///     </remarks>
        ///     <param name="settings">The Hashtable to read from.</param>
        ///     <param name="key">A key for the value in the Hashtable.</param>
        ///     <param name="defaultValue">The default value if the item is not found.</param>
        ///     <retvalue>
        ///		    <para>value: from the hash table</para>
        ///         <para>
        ///             default: if the item is not in the table or cannot be case to the expected type.
        ///		    </para>
        ///	    </retvalue>
        /// </summary>
        public static String ReadSetting(NameValueCollection settings, String key, String defaultValue)
        {
            try
            {
                Object setting = settings[key];

                return (setting == null) ? defaultValue : (String)setting;
            }
            catch
            {
                return defaultValue;
            }
        }

        /// <summary>
        ///     Boolean version of ReadSetting.
        ///     <remarks>
        ///         Reads a setting from a hashtable and converts it to the correct
        ///         type. One of these functions is provided for each type
        ///         expected in the hash table. These are public so that other
        ///         classes don't have to duplicate them to read settings from
        ///         a hash table.
        ///     </remarks>
        ///     <param name="settings">The Hashtable to read from.</param>
        ///     <param name="key">A key for the value in the Hashtable.</param>
        ///     <param name="defaultValue">The default value if the item is not found.</param>
        ///     <retvalue>
        ///		    <para>value: from the hash table</para>
        ///         <para>
        ///             default: if the item is not in the table or cannot be case to the expected type.
        ///		    </para>
        ///	    </retvalue>
        /// </summary>
        public static bool ReadSetting(NameValueCollection settings, String key, bool defaultValue)
        {
            try
            {
                Object setting = settings[key];

                return (setting == null) ? defaultValue : Convert.ToBoolean((String)setting);
            }
            catch
            {
                return defaultValue;
            }
        }

        /// <summary>
        ///     int version of ReadSetting.
        ///     <remarks>
        ///         Reads a setting from a hashtable and converts it to the correct
        ///         type. One of these functions is provided for each type
        ///         expected in the hash table. These are public so that other
        ///         classes don't have to duplicate them to read settings from
        ///         a hash table.
        ///     </remarks>
        ///     <param name="settings">The Hashtable to read from.</param>
        ///     <param name="key">A key for the value in the Hashtable.</param>
        ///     <param name="defaultValue">The default value if the item is not found.</param>
        ///     <retvalue>
        ///		    <para>value: from the hash table</para>
        ///         <para>
        ///             default: if the item is not in the table or cannot be case to the expected type.
        ///		    </para>
        ///	    </retvalue>
        /// </summary>
        public static int ReadSetting(NameValueCollection settings, String key, int defaultValue)
        {
            try
            {
                Object setting = settings[key];

                return (setting == null) ? defaultValue : Convert.ToInt32((String)setting);
            }
            catch
            {
                return defaultValue;
            }
        }
        #endregion

    }
}
